namespace KinonekoSoftware.Studio.Components
{
    public sealed class RepositorySelectorViewModel : SelectionDialog<RepositoryCache>
    {
        public static async Task<Result<RepositoryCache>> Select(IEnumerable<RepositoryCache> repositories)
        {
            var context = new DialogContext();
            context.Set(ContextBase.Key_Parameter, false);
            context.Set(ContextBase.Key_Value, repositories);


            var r = await Dialog((ISelectionDialog<RepositoryCache>)new RepositorySelectorViewModel(), context);

            if (r.IsFinished)
            {
                return Result<RepositoryCache>.Success(r.Value.FirstOrDefault());
            }

            return Result<RepositoryCache>.Failure;
        }

        protected override bool OnReceive(DialogContext context)
        {
            if (context.Has(ContextBase.Key_Parameter) && context.Has(ContextBase.Key_Value))
            {
                var c = context.GetObject<IEnumerable<RepositoryCache>>(ContextBase.Key_Value);
                IsMultipleSelection = context.GetBoolean(ContextBase.Key_Parameter);
                Collection.AddMany(c, true);
            }
            
            return base.OnReceive(context);
        }
    }
}